package mx.netbrain.dao.impl;

import java.util.List;

import mx.netbrain.dao.RepositoryGroupDao;
import mx.netbrain.model.Groups;
import mx.netbrain.model.Repositories;
import mx.netbrain.model.RepositoriesGroups;
import mx.netbrain.util.HibernateUtil;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;

public class RepositoryGroupDaoImpl implements RepositoryGroupDao {

	private static final Logger logger = Logger
			.getLogger(RepositoryGroupDaoImpl.class);

	@SuppressWarnings("unchecked")
	@Override
	public List<Groups> findGroupRepository(Repositories repo) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String sql = "select repositoriesgroups.groups from RepositoriesGroups as "
				+ "repositoriesgroups where repositoriesgroups.repositories = :repo";
		Query query = session.createQuery(sql);
		query.setLong("repo", repo.getId());
		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Groups> findGroupNoRepository(Repositories repo) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String sql = "select groups from Groups as groups where groups.id not in "
				+ "(select repositoriesgroups.groups.id from RepositoriesGroups as repositoriesgroups "
				+ "where repositoriesgroups.repositories = :repo) "
				+ "and groups.status = 1";
		Query query = session.createQuery(sql);
		query.setLong("repo", repo.getId());
		return query.list();
	}

	@Override
	public void delete(Groups group, Repositories repo) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		String sql = "delete from RepositoriesGroups as repositoriesgroups where "
				+ "repositoriesgroups.groups = :group and repositoriesgroups.repositories = :repo";
		Query query = session.createQuery(sql);
		query.setLong("group", group.getId());
		query.setLong("repo", repo.getId());
		query.executeUpdate();
		session.beginTransaction().commit();
	}

	@Override
	public void save(RepositoriesGroups repoGroup) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.persist(repoGroup);
			session.beginTransaction().commit();
		} catch (Exception e) {
			logger.error("Save: " + e.getMessage());
			session.beginTransaction().rollback();
		}
	}

	@Override
	public RepositoriesGroups find(Groups group, Repositories repo) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String sql = "select repositoriesgroups from RepositoriesGroups as repositoriesgroups where "
				+ "repositoriesgroups.groups = :group and repositoriesgroups.repositories = :repo";
		Query query = session.createQuery(sql);
		query.setLong("group", group.getId());
		query.setLong("repo", repo.getId());
		if ((RepositoriesGroups) query.uniqueResult() != null) {
			return (RepositoriesGroups) query.uniqueResult();
		} else {
			logger.warn("No se encontro ningun registro");
			return (RepositoriesGroups) query.uniqueResult();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Groups> findGroupForRepository(Repositories repo) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String sql = "select repositoriesgroups.groups from RepositoriesGroups as repositoriesgroups "
				+ "where repositoriesgroups.repositories = :repo";
		Query query = session.createQuery(sql);
		query.setLong("repo", repo.getId());
		return query.list();
	}

}
